package com.example.myretrofit2.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myretrofit2.Car;
import com.example.myretrofit2.Goods;
import com.example.myretrofit2.R;
import com.example.myretrofit2.activity.GoodsInfoActivity;
import com.example.myretrofit2.activity.GoodsList;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.presenter.CarPresenter;
import com.example.myretrofit2.presenter.MySubscriber;
import com.nostra13.universalimageloader.core.ImageLoader;


public class GoodsFragment extends Fragment implements View.OnClickListener{
ImageView imageView;
TextView goodsName;
TextView goodsPrice;
Button add;
Button sub;
TextView num;
Button add_car;
Goods goods;
DrawerLayout drawerLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_goods, container, false);
        Bundle bundle = getArguments();
        goods = (Goods) bundle.getParcelable("goods");
        drawerLayout = view.findViewById(R.id.goods_draw);
        imageView = view.findViewById(R.id.goods_img_dw);
        goodsName = view.findViewById(R.id.goods_name_dw);
        goodsPrice = view.findViewById(R.id.goods_price_dw);
        add = view.findViewById(R.id.add);
        sub = view.findViewById(R.id.sub);
        add_car = view.findViewById(R.id.add_car);
        num = view.findViewById(R.id.num);
        add_car.setOnClickListener(this);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        ImageLoader.getInstance().displayImage(MyApplication.imageUrl+goods.getSrc(),imageView);
        goodsName.setText(goods.getGoodname());
        goodsPrice.setText("￥"+goods.getPrice());
        return view;
    }

    public void onClick(final View view){
        int count = Integer.parseInt(num.getText().toString());
        switch (view.getId()){
            case R.id.add:
                num.setText(count+1+"");
                break;
            case R.id.sub:
                if(count>1) {
                    num.setText(count - 1 + "");
                }
                else {

                }
                break;
            case R.id.add_car:
//                Intent intent = new Intent();
//                intent.setAction("com.example.myretrofit2");
//                intent.putExtra("count",count);
//                getContext().sendBroadcast(intent);

                Car car = new Car();
                car.setGoodId(goods.getId());
                car.setGoodName(goods.getGoodname());
                car.setPrice(goods.getPrice());
                car.setNum(count);
                car.setImg(goods.getSrc());
                CarPresenter.insertCar(new MySubscriber<Boolean>(MyApplication.getContext()) {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        if(aBoolean){
                            Toast.makeText(getContext(),"加入购物车成功",Toast.LENGTH_SHORT).show();
                        }
                    }
                },car);
                drawerLayout.closeDrawers();
                break;
        }
    }


}